Skip to content

UI: TextureRenderCore's drawBitmapFaceWithBlendUnk at 100% - #494

Merged
gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-C-round23-100
Nov 25, 2026
Merged

gynt merged 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-C-round23-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 100%, rewritten from the binary. No header changes, no /GL line.

Function Address Before reccmp raw Normalised
TextureRenderCore::drawBitmapFaceWithBlendUnk 0x0044CEB0 0.0% Pending (gynt's draft a5ef761 reads 87.4% built on main) 100.0% 100.0%

gynt's draft is a5ef761 (2026-10-04, "reimplement: SHC_3BB0A8C1_0x0044CEB0 87.1% ushort pixel locals, per-channel grouping and signed division by 0x20") on sourcehold's reimpl/TextureRenderCore. Its per-channel grouping and the signed division by 32 are kept here; the rest is written again in the shape of drawBitmapFace (0x0044CD40, on main), which draws the same face without blending.

Measured

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed).
  • Narrow build: this commit's file together with drawBitmapFace.cpp (its one callee, 100% on main), on main 1f87a13: 100.0 raw and normalised. The whole listing is the same as the original's 409 instructions. drawBitmapFace stays at 100.0. With this file on the /GL list it also reads 100.0, so the PR adds no /GL line.
  • Full build: an all-on build of this branch on main 1f87a13, with both resolver sets on and main's own /GL list, and with requestGoods (0x004D1F00) and MenuItemActionHandler_Allies_Main (0x004B12D0) left out with their resolvers off (they still use the two PlayerData fields Reimplement 29 Synchrony and Game functions at 100% #403 renames), together with hasEnoughGoldForMoat (0x0040CAC0), which doesn't compile on main until Reimplement 88 Map::TileMapState functions at 100% #366 (its header returns BOOLEnum since the Reimplement 88 Map::TileMapState functions at 100% #366 sync, the file uint): 0 failed of 2,655 compile steps, 99.68 over 2,671 functions. drawBitmapFaceWithBlendUnk reads 100.0 raw and normalised there, with no diff entry, and every other function reads exactly what it reads in the same build of main (unrounded 99.6839 raw and 99.6456 normalised, 2,563 functions at 100.0 raw; main alone: 99.6838 and 99.6455 over 2,670, 2,562 at 100.0).
  • Checks: our local style checker: 0 ERROR, 3 CHECK (SG 24, below); main's style_lint reads 0; the fixer has nothing to change. clang-format 23.1.3 changes nothing.
  • Not run in game.

What the function does

It draws one 64 x 66 face bitmap at a position, blended by a strength from 0 to 32. A strength of 0 draws the face plainly through drawBitmapFace, and 32 draws nothing. It draws on the surface that drawBufferChoiceValue picks, as drawBitmapFace does, and skips the magenta colour key. In RGB565 each channel is the face's weighted by 32 - strength plus the surface pixel's weighted by the strength, out of 32. In RGB555 the face's weight is the strength itself, the reverse of RGB565, and the surface under it is left out: a strength of 1 draws the face almost black and 31 almost unchanged. The code does what the original does and marks it fixme.

For the maintainers: the RGB555 weight looks like a bug in the original (inverted against the RGB565 path). It is kept, since the function matches the binary.

What got it to 100%

Each of these was measured against the form it names:

  • Plain for loops over the 66 rows and 64 columns. MSVC unrolls the RGB565 loop by four itself. That gives the original's four pixels per iteration, with the face read at [ebx] to [ebx+6] and one add ebx, 8. It also gives the original's channel order, green before red: with the four pixels written out by hand, as in the draft, every pixel computes red first, whatever order the source writes the channels in (the draft reads 87.4%).
  • In the RGB565 loop the surface pointer steps in both branches of the colour-key test. The original steps it by 2 after every pixel. With one ++pixel behind the test, the unrolled copies write through [ecx+2], [ecx+4] and [ecx+6] and step it by 8 instead (86.8%). The RGB555 loop is not unrolled and has the single step of drawBitmapFace (the same listing either way).
  • One local per channel in the RGB555 loop as well. With the RGB555 blend as one expression, the RGB565 loop computes red before green again (92.2%). The two loops share no code, so the order follows from the whole function rather than from the expression; the comment says so in one line.
  • faceWeight before the surface switch, where the original computes 32 - strength (0x0044CEF6). Declared inside the RGB565 branch, where it is used, it reads 94.6 / 79.4.

Checker items (not changed)

  • 3 x SG 24 (declared without a value): surface, rowSkip and lineByteWidth are set in each arm of the surface switch and read after it. rowSkip and lineByteWidth are declared the same way in drawBitmapFace. surface is a local here because this original keeps the surface in a register and stores the global once after the switch (0x0044CF5B), where drawBitmapFace stores the global in each arm.

Proposed, not changed here

Other PRs

🤖 Generated with Claude Code

Rewritten from the binary. gynt's draft a5ef761 (sourcehold reimpl/TextureRenderCore, 87.1% there, 87.4% on
main) was the starting point; its per-channel grouping and the signed division by 32 are kept.

- Both loops are plain for loops over the face's 66 rows and 64 columns, as in drawBitmapFace. MSVC unrolls the
  RGB565 one by four itself, which gives the original's four pixels per iteration and its channel order.
- In the RGB565 loop the surface pointer steps in both branches of the colour-key test, as the original steps it
  after every pixel. With one step behind the test, the unrolled copies write through offsets instead.
- The RGB555 blend has one local per channel: as one expression, the RGB565 loop computes red before green.
- The RGB555 path weighs the face by the strength, not 32 - strength, and leaves out the surface, as the original
  does; it is marked fixme.
- The masks are main's ColorChannelMask constants.

Narrow build with drawBitmapFace (100% on main): 100.0 raw and normalised, the original's 409 instructions. It
needs no /GL line.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gynt
gynt added this pull request to the merge queue Nov 25, 2026
Merged via the queue into sourcehold:main with commit d235784 Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants